﻿<!DOCTYPE html>
<html>
<head>
    <title>Noria Logging</title>
    <script src="js/jquery.js"></script>

    <script>
        $(function () {

            String.prototype.format = function () {
                var args = arguments;
                return this.replace(/\{(\d+)\}/g,
                    function (m, i) {
                        return args[i];
                    });
            };

            var cout = $("#cout");
            var list = $("#list");

            $.ajax({
                type: "POST",
                url: "log.ashx",
                contentType: "application/json",
                data: JSON.stringify({}),
                beforeSend: function () {
                    //button.attr("disabled", "disabled");
                },
                error: function (xhr, text, err) {
                    try {
                        cout.html(JSON.parse(xhr.responseText).error).show();
                    } catch (e) {
                        cout.html(xhr.statusText).show();
                    }
                },
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var log = data[i];
                        list.append('<li name="{0}">{0}, {1} <a href="#">500</a> <a href="#">all</a></li>'.format(log.name, log.size));                        
                    }
                    list.find("li").each(function () {
                        $(this).find("a").first().click(function () {

                            var name = $(this).parent().attr("name");
                            var size = 500;
                            cout.html("");

                            $.ajax({
                                type: "POST",
                                url: "log.ashx?name={0}&size={1}".format(name, size),
                                contentType: "application/json",
                                data: JSON.stringify({}),
                                error: function (xhr, text, err) {
                                    try {
                                        cout.html(JSON.parse(xhr.responseText).error).show();
                                    } catch (e) {
                                        cout.html(xhr.statusText).show();
                                    }
                                },
                                success: function (data2) {
                                    for (var i = 0; i < data2.length; i++) {
                                        var line = data2[i];
                                        cout.append('<pre>{0}</pre>'.format(line));
                                    }
                                }
                            });
                        });
                        $(this).find("a").last().click(function () {

                            var name = $(this).parent().attr("name");
                            var size = -1;
                            cout.html("");

                            $.ajax({
                                type: "POST",
                                url: "log.ashx?name={0}&size={1}".format(name, size),
                                contentType: "application/json",
                                data: JSON.stringify({}),
                                error: function (xhr, text, err) {
                                    try {
                                        cout.html(JSON.parse(xhr.responseText).error).show();
                                    } catch (e) {
                                        cout.html(xhr.statusText).show();
                                    }
                                },
                                success: function (data2) {
                                    for (var i = 0; i < data2.length; i++) {
                                        var line = data2[i];
                                        cout.append('<pre>{0}</pre>'.format(line));
                                    }
                                }
                            });
                        });
                    });
                },
                complete: function () {
                    //button.removeAttr("disabled");
                }
            });
        });
    </script>
</head>
<body>
    <h1>Noria Logging</h1>
    <ul id="list">
    </ul>
    <div id="cout"></div>
</body>
</html>
